
<%
metamodel http://www.eclipse.org/uml2/2.0.0/UML

import org.acceleo.modules.uml2.services.Uml2Services
import org.acceleo.modules.uml2.services.StringServices
%>

<%-- 
This template deals with Struts actions
--%>

<%script type="Class" name="fullFilePath"%>
<%-- Only Classes with stereotype Screen--%>
<%if (hasStereotype("Entity")){%>
/<%scenarioPackage.toPath()%>/<%name%>Control.java
<%}%>

<%script type="Class" name="generate" file="<%fullFilePath%>"%>
<%-- Generate Struts actions--%>
package <%scenarioPackage%>;


import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.acceleo.sample.dto.<%name%>Dto;
import org.acceleo.sample.jdbc.Jdbc<%name%>Dao;
import org.acceleo.fwk.dao.DaoException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * Implementation of <strong><%name%> Servlet</strong>
 * Scenario : <%package.name%>
 *
 */
public class <%name%>Control extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
    /**
     * Commons Logging instance.
     */
	private static final Log log = LogFactory.getLog(<%name%>Control.class);

    /**
     * Forward codes.
     */
	public final static String PAGE_SELF = "<%package.name%>/<%name%>Form.jsp"; //$NON-NLS-1$
	public final static String PAGE_ADMIN = "JSPAdmin.jsp"; //$NON-NLS-1$
<%-- class association navigation --%>


<%-- init action method generation (Dispach)--%>
    /**
     * Process the specified HTTP request for event.
     * @param requ The HTTP request we are processing
     * @param resp The HTTP response we are creating
     *
     * @exception Exception if an input/output error or servlet exception occurs
     */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doAction(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doAction(req, resp);
	}
	
	protected void doAction(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String event = req.getParameter("action");
		log.debug("Find event : " + event);
		// <%startUserCode%> before calling specific action
		// TOFILL Write here the code to execute before any action of <%name%>Control servlet
		// <%endUserCode%>
		
		if ( event == null ) {
			init(req, resp);
		}		
		else{
			String redirect=req.getParameter("redirect");
			if (redirect==null) redirect=PAGE_SELF;
			else if ("JSPAdmin".equals(redirect)) redirect=PAGE_ADMIN;
			else redirect=PAGE_SELF;
			
			if ("createTable".equals(event)) {
				this.createTable(req,resp,redirect);
			}
			else if ("addElement".equals(event)) {
				this.addElement(req,resp,redirect);
			}
			else if ("deleteTable".equals(event)) {
				this.deleteTable(req,resp,redirect);
			}
			else{
			redirect(PAGE_SELF,req,resp);
			}
		}
	}
	
    /**
     * Process the specified HTTP request for the initial call to this servlet.
     * @param req The HTTP request we are processing
     * @param resp The HTTP response we are creating
     *
     * @exception Exception if an input/output error or servlet exception occurs
     */
	public void init(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		log.debug("Starting <%name%>.init");
		
		String returnCode = PAGE_SELF;
		//<%startUserCode%> init
		// TODO Write here the action code for <%name%>
		//<%endUserCode%>		
		redirect(returnCode,req,resp);
	}

<%-- actions methods generation --%>

    /**
     * Create the entity.
     * @param req The HTTP request we are processing
     * @param resp The HTTP response we are creating
     *
     * @exception Exception if an input/output error or servlet exception occurs
     */
	public void addElement(HttpServletRequest req, HttpServletResponse resp,String redirect) throws ServletException, IOException {
		log.debug("Starting <%name%>.addElement");
		<%for (attribute){%>
			<%if (association == null){%>
		String <%name%> = req.getParameter("<%name%>");
			<%}%>
		<%}%>
		
		String returnCode = redirect;
		
		<%name%>Dto newDto = new <%name%>Dto();
		<%for (attribute){%>
			<%if (association == null){%>			
				<%if (type.name=="String"){%>
					newDto.set<%name.firstUpper()%>(<%name%>);
				<%}%>									
			<%}%>
		<%}%>		
				
		
		Jdbc<%name%>Dao newDao = new Jdbc<%name%>Dao();
		try {
			newDao.create<%name%>(newDto);
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	redirect(returnCode,req,resp);
		
	}
	
	private void deleteTable(HttpServletRequest req, HttpServletResponse resp,String redirect) throws ServletException, IOException {
		log.debug("Starting <%name%>.deleteTable");
		new Jdbc<%name%>Dao().deleteTable();	
		redirect(redirect,req,resp);
	}
	
	private void createTable(HttpServletRequest req, HttpServletResponse resp,String redirect) throws ServletException, IOException {
		log.debug("Starting <%name%>.createTable");
		new Jdbc<%name%>Dao().createTable();	
		redirect(redirect,req,resp);
	}

	private void redirect(String returnCode, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
	RequestDispatcher dispatcher = req.getRequestDispatcher("/jsp/" + returnCode);
		if (dispatcher != null) {
			//dispatcher.forward(req, resp);
			resp.sendRedirect(getServletContext().getContextPath()+"/jsp/" + returnCode);
		} else {
			throw new ServletException("Redirection failed because " + returnCode + " hasn't been found");
		}
}

	// <%startUserCode%> user methods
	// <%endUserCode%>

}

<%script type="Class" name="scenarioPackage"%>
org.taam.test.servlets


